from openpyxl import Workbook
from openpyxl.chart import (
    Reference,
    BarChart,
)

wb = Workbook()
ws = wb.active

rows = [
    ['交换机', '5秒钟', '1分钟', '5分钟'],
    ['1.1.1.1', 1, 1, 1],
    ['2.2.2.2', 2, 2, 2],
    ['3.3.3.3', 3, 3, 3],
    ['4.4.4.4', 4, 4, 4],
]

for row in rows:
    ws.append(row)

# 数据row从1-5共取4行数据为4组数据，col从2-4列展示3行数据类型。

data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=len(rows))
# data = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=5)
# 每组数据展示的标题，min_col取每行数据的第一列，row从2-5从1.1.1.1到4.4.4.4。
titles = Reference(ws, min_col=1, min_row=2, max_row=len(rows))
# titles = Reference(ws, min_col=1, min_row=2, max_row=5)
chart = BarChart()
chart.title = "交换机内存使用率"
chart.add_data(data=data, titles_from_data=True)
chart.set_categories(titles)

ws.add_chart(chart, "E5")
wb.save("g:\\bar3d.xlsx")